package com.yc.mqtt.utils;

import com.yc.mqtt.exception.MqttException;

import java.util.concurrent.ConcurrentHashMap;

public class ShareUtils {


    private static ConcurrentHashMap<Class, Object> beans = new ConcurrentHashMap<Class, Object>();

    public static <T> T getShareObject(Class<T> claz) {
        try {
            Object object = beans.get(claz);
            if (null != object) {
                return (T) object;
            } else {
                T bean = (T) claz.newInstance();
                beans.putIfAbsent(claz, bean);
                return bean;
            }
        } catch (Exception e) {
            throw new MqttException("can not init " + claz);
        }
    }
}
